﻿using Microsoft.AspNetCore.Mvc;
using MyWeb.ScheduledTasks.Business;
using MyWeb.ScheduledTasks.Models;
using Quartz.Spi;
using Quartz;
using MyWeb.ScheduledTasks.Services;
using Mapster;
using static MyWeb.ScheduledTasks.Protos.ResponseJob.Types;
using MyWeb.ScheduledTasks.Protos;

namespace MyWeb.ScheduledTasks.Controllers
{
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class ScheduledController : Controller
    {
        private ISchedulerFactory _schedulerFactory;
        private IJobFactory _jobFactory;
        QuartzService _quartz;

        private readonly IJobsService _service;

        public ScheduledController(ISchedulerFactory schedulerFactory, IJobFactory jobFactory, IJobsService service)
        {
            _schedulerFactory = schedulerFactory;
            _jobFactory = jobFactory;
            _service = service;
            _quartz = new QuartzService(_service, _schedulerFactory, _jobFactory);
        }
        /// <summary>
        /// 初始化所有作业
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Init()
        {
            await _quartz.InitJobs(default, default);
            return Ok();
        }

        /// <summary>
        /// 查询所有作业
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Get(int page = 1,int pageCount = 2)
        {
            //实体生成数据库表
            //BaseDB.CodeFirst.InitTables(typeof(Jobs));//具体用法看文档迁移
            //return Ok(await _quartz.GetJobs(new Google.Protobuf.WellKnownTypes.Empty(),null));
            return Ok(await _service.GetJobs(page,pageCount));
        }
        /// <summary>
        /// 添加作业任务
        /// </summary>
        /// <param name="jobs"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> AddJob(Jobs jobs)
        {
            return Ok(await _quartz.AddJob(jobs.Adapt<JobsModel>(),null));
        }
        /// <summary>
        /// 移除任务
        /// </summary>
        /// <param name="jobId">任务Id</param>
        /// <param name="taskName">任务名称</param>
        /// <param name="groupName">任务组名</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Remove(long jobId = 1671088025716789248, string? taskName = "", string? groupName = "")
        {
            return Ok(await _quartz.Remove(new JobsModel() { Id = jobId, TaskName = taskName, GroupName = groupName }, null));
        }
        /// <summary>
        /// 修改任务信息
        /// </summary>
        /// <param name="jobs"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Update(Jobs jobs)
        {
            return Ok(await _quartz.Update(jobs.Adapt<JobsModel>(), null));
        }
        /// <summary>
        /// 暂停任务
        /// </summary>
        /// <param name="jobId"></param>
        /// <param name="taskName"></param>
        /// <param name="groupName"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Pause(long jobId = 1671088025716789248, string? taskName = "", string? groupName = "")
        {
            return Ok(await _quartz.Pause(new JobsModel() { Id = jobId, TaskName = taskName, GroupName = groupName }, null));
        }
        /// <summary>
        /// 开始执行任务
        /// </summary>
        /// <param name="jobId"></param>
        /// <param name="taskName"></param>
        /// <param name="groupName"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Start(long jobId = 1671088025716789248, string? taskName = "", string? groupName = "")
        {
            TaskRequest request = new TaskRequest() { JobId = jobId, TaskName = taskName, GroupName = groupName };
            return Ok(await _quartz.Start(request,null));
        }
        /// <summary>
        /// 运行一次任务
        /// </summary>
        /// <param name="jobId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Run(long jobId = 1671088025716789248)
        {
            return Ok(await _quartz.Run(new JobsModel() { Id = jobId},null));
        }
        /// <summary>
        /// 查看任务状态
        /// </summary>
        /// <param name="jobId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetState(long jobId = 1671088025716789248)
        {
            return Ok(await _quartz.GetState(new JobsModel() { Id = jobId }, null));
        }
        /// <summary>
        /// 查看任务是否存在
        /// </summary>
        /// <param name="jobId"></param>
        /// <param name="taskName"></param>
        /// <param name="groupName"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> IsQuartzJob(long jobId = 1671088025716789248, string? taskName = "", string? groupName = "")
        {
            return Ok(await _quartz.IsQuartzJob(new TaskRequest() { JobId = jobId, TaskName = taskName, GroupName = groupName }, null));
        }
        /// <summary>
        /// 校验Cron表达式
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> IsValidExpression(string str)
        {
            return Ok(await _quartz.IsValidExpression(new ResponseString() { Value = str }, null));
        }
    }
}
